package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.LinkMapper;
import com.itheima.pojo.Link;
import com.itheima.service.LinkService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class LinkServiceImpl implements LinkService {

    @Autowired
    private LinkMapper linkMapper;

    @Override
    public void add(Link link) {
        linkMapper.insert(link);
    }

    @Override
    public void delete(Link link) {
        linkMapper.delete(link);
    }

    @Override
    public void edit(Link link) {
        linkMapper.updateByPrimaryKey(link);
    }


    @Override
    public void stick(Link link) {
        Integer maxSort = linkMapper.getMaxSort();
        link.setSort(maxSort + 1);
        linkMapper.updateByPrimaryKeySelective(link);
    }

    @Override
    public Page<Link> getList(Map<String, Object> searchMap) {
        Integer currentPage = (Integer) searchMap.get("currentPage");
        if (currentPage == null) {
            currentPage = 1;
        }
        Integer pageSize = (Integer) searchMap.get("pageSize");
        if (pageSize == null) {
            pageSize = 10;
        }
        PageHelper.startPage(currentPage, pageSize);
        Example example = createExample(searchMap);
        return (Page<Link>) linkMapper.selectByExample(example);
    }

    // web获取友链列表
    @Override
    public List<Link> getListByPageSize(Integer pageSize) {
        PageHelper.startPage(1, pageSize);
        Example example = new Example(Link.class);
        example.setOrderByClause("sort desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("link_status", 1);
        criteria.andEqualTo("status", 1);
        List<Link> linkList = linkMapper.selectByExample(example);
        return linkList;
    }

    // 增加友情链接点击数
    @Override
    public void addLinkCount(String uid) {
        if (StringUtils.isNotEmpty(uid)) {
            Link link = linkMapper.selectByPrimaryKey(uid);
            if (link != null) {
                Integer clickCount = link.getClickCount() + 1;
                link.setClickCount(clickCount);
                linkMapper.updateByPrimaryKeySelective(link);                
            }
        }
    }


    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Link.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {

            if (searchMap.get("keyword") != null && !"".equals(searchMap.get("keyword"))) {
                criteria.andLike("title", "%" + searchMap.get("keyword") + "%");
            }
            if (searchMap.get("linkStatus") != null && !"".equals(searchMap.get("linkStatus"))) {
                criteria.andEqualTo("linkStatus", searchMap.get("linkStatus"));
            }
            if (searchMap.get("title") != null && !"".equals(searchMap.get("title"))) {
                criteria.andEqualTo("title", searchMap.get("title"));
            }
            if (searchMap.get("summary") != null && !"".equals(searchMap.get("summary"))) {
                criteria.andEqualTo("summary", searchMap.get("summary"));
            }
            if (searchMap.get("clickCount") != null && !"".equals(searchMap.get("clickCount"))) {
                criteria.andEqualTo("clickCount", searchMap.get("clickCount"));
            }
            if (searchMap.get("uid") != null && !"".equals(searchMap.get("uid"))) {
                criteria.andEqualTo("uid", searchMap.get("uid"));
            }
            if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }
            if (searchMap.get("url") != null && !"".equals(searchMap.get("url"))) {
                criteria.andEqualTo("url", searchMap.get("url"));
            }
            if (searchMap.get("sort") != null && !"".equals(searchMap.get("sort"))) {
                criteria.andEqualTo("sort", searchMap.get("sort"));
            }
        }
        return example;
    }
}
